package cn.bigfire.wedesk.net.fivesocket.frame;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

public class ContinueFiveFrame extends AbsFiveFrame {

    /**
     * Creates a new empty continuation frame.
     */
    public ContinueFiveFrame() {
        this(Unpooled.buffer(0));
    }

    /**
     * Creates a new continuation frame with the specified binary data. The final fragment flag is
     * set to true.
     *
     * @param binaryData the content of the frame.
     */
    public ContinueFiveFrame(ByteBuf binaryData) {
        super(binaryData);
    }

    /**
     * Creates a new continuation frame with the specified binary data.
     *
     * @param finalFragment
     *            flag indicating if this frame is the final fragment
     * @param rsv
     *            reserved bits used for protocol extensions
     * @param binaryData
     *            the content of the frame.
     */
    public ContinueFiveFrame(boolean finalFragment, int rsv, ByteBuf binaryData) {
        super(finalFragment, rsv, binaryData);
    }

    /**
     * Creates a new continuation frame with the specified text data
     *
     * @param finalFragment
     *            flag indicating if this frame is the final fragment
     * @param rsv
     *            reserved bits used for protocol extensions
     * @param text
     *            text content of the frame.
     */
    public ContinueFiveFrame(boolean finalFragment, int rsv, String text) {
        this(finalFragment, rsv, fromText(text));
    }

    /**
     * Returns the text data in this frame.
     */
    public String text() {
        return content().toString(CharsetUtil.UTF_8);
    }

    /**
     * Sets the string for this frame.
     *
     * @param text
     *            text to store.
     */
    private static ByteBuf fromText(String text) {
        if (text == null || text.isEmpty()) {
            return Unpooled.EMPTY_BUFFER;
        } else {
            return Unpooled.copiedBuffer(text, CharsetUtil.UTF_8);
        }
    }

    @Override
    public ContinueFiveFrame copy() {
        return (ContinueFiveFrame) super.copy();
    }

    @Override
    public ContinueFiveFrame duplicate() {
        return (ContinueFiveFrame) super.duplicate();
    }

    @Override
    public ContinueFiveFrame retainedDuplicate() {
        return (ContinueFiveFrame) super.retainedDuplicate();
    }

    @Override
    public ContinueFiveFrame replace(ByteBuf content) {
        return new ContinueFiveFrame(isFinalFragment(), rsv(), content);
    }

    @Override
    public ContinueFiveFrame retain() {
        super.retain();
        return this;
    }

    @Override
    public ContinueFiveFrame retain(int increment) {
        super.retain(increment);
        return this;
    }

    @Override
    public ContinueFiveFrame touch() {
        super.touch();
        return this;
    }

    @Override
    public ContinueFiveFrame touch(Object hint) {
        super.touch(hint);
        return this;
    }
}
